package com.example.app12_data_storage.activity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.app12_data_storage.R;
import com.example.app12_data_storage.utils.DbHelper;

/**
 * 演示操作Sqlite数据库
 */
public class SqliteActivity extends AppCompatActivity {

    private static final String TAG = "SqliteActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_sqlite);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.ll_sqlite), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Log.i(TAG, "onCreate()");


    }

    /**
     * 数据库创建
     * @param view 发生点击事件的视图对象
     */
    public void createDb(View view) {
        try (DbHelper dbHelper = new DbHelper(this, 1)) {
            SQLiteDatabase db = dbHelper.getReadableDatabase();
        }
        Toast.makeText(this, "数据库创建成功~", Toast.LENGTH_SHORT).show();
    }

    /**
     * 更新数据库
     * @param view 发生点击事件的视图对象
     */
    public void upgradeDb(View view) {
        try (DbHelper dbHelper = new DbHelper(this, 2)) {
            SQLiteDatabase db = dbHelper.getReadableDatabase();
        }
        Toast.makeText(this, "数据库更新成功~", Toast.LENGTH_SHORT).show();
    }

    /**
     * 新增
     * @param view 发生点击事件的视图对象
     */
    public void insert(View view) {
        try (DbHelper dbHelper = new DbHelper(this, 2);
             SQLiteDatabase db = dbHelper.getReadableDatabase()) {
            ContentValues values = new ContentValues();
            values.put("name", "jerry");
            values.put("age", "5");
            long personId = db.insert("person", null, values);
            Toast.makeText(this, "新增数据成功：" + personId, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 修改
     * @param view 发生点击事件的视图对象
     */
    public void update(View view) {
        try (DbHelper dbHelper = new DbHelper(this, 2);
             SQLiteDatabase db = dbHelper.getReadableDatabase()) {
            ContentValues values = new ContentValues();
            values.put("name", "jack");
            values.put("age", "15");
            int updatedCount = db.update("person", values, "_id=?", new String[]{"2"});
            Toast.makeText(this, "更新数据成功：" + updatedCount, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 删除
     * @param view 发生点击事件的视图对象
     */
    public void delete(View view) {
        try (DbHelper dbHelper = new DbHelper(this, 2);
             SQLiteDatabase db = dbHelper.getReadableDatabase()) {
            int deletedCount = db.delete("person", "_id=?", new String[] { "2" });
            Toast.makeText(this, "删除数据成功：" + deletedCount, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 查询
     * @param view 发生点击事件的视图对象
     */
    public void query(View view) {
        try (DbHelper dbHelper = new DbHelper(this, 2);
             SQLiteDatabase db = dbHelper.getReadableDatabase()) {
            Cursor person = db.query("person", null, null, null, null, null, null);
            while (person.moveToNext()) {
                int id = person.getInt(0);
                String name = person.getString(1);
                int age = person.getInt(2);
                Log.i(TAG, "person[id: " + id + ", name:" + name + ", age: " + age + "]");
            }
            int count = person.getCount();
            person.close();
            Toast.makeText(this, "查询数据成功~" + count, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 事务
     * @param view 发生点击事件的视图对象
     */
    public void transaction(View view) {
        SQLiteDatabase db = null;
        try {
            DbHelper dbHelper = new DbHelper(this, 2);
            db = dbHelper.getReadableDatabase();

            // 开启事务
            db.beginTransaction();

            ContentValues values = new ContentValues();
            values.put("age", "17");
            db.update("person", values, "_id=?", new String[]{"1"});

            values = new ContentValues();
            values.put("age", "18");
            db.update("person", values, "_id=?", new String[]{"2"});

            Toast.makeText(this, "更新数据成功~", Toast.LENGTH_SHORT).show();
            db.setTransactionSuccessful();
        } finally {
            if (db != null) {
                db.endTransaction();
                db.close();
            }
        }
    }
}